<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Setting extends CH_Controller{

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	 /**
         * Constructor
         */
	function __construct() { 
		parent::__construct(); 
		$this->init();
		$this->load->helper(array('directory','file'));
		$this->load->model('setting_model');
		
    } 
    	
	
	/**
	 * 사이트 정보
	 *
	 * @access	public
	 * @return	data
	 */
	function siteinfo(){
		login_admin();
		$url = $this->uri_church();
		$data = array(
				'_title' => '사이트정보',
				'_group' => 'setting',
				'_content' => $this->config->item('ch_manager_root').'/'.$url
		);
		
		$data = array_merge($data,$this->setting_model->select_siteinfo());
		return $data ;
	}
	
	/**
	 * 사이트 정보 DB 처리
	 *
	 * @access	public
	 * @return	void
	 */
	function siteinfoDbjob(){
		login_admin();
		$dbjob = $this->input->post('dbjob');
		
		$zipcode1 = $this->input->post('zipcode1');
		$zipcode2 = $this->input->post('zipcode2');
		$post = $zipcode1.$zipcode2;
		
		$tel1 = $this->input->post('tel1');
		$tel2 = $this->input->post('tel2');
		$tel3 = $this->input->post('tel3');
		$tel = "{$tel1}-{$tel2}-{$tel3}"; 
		
		$fax1 = $this->input->post('fax1');
		$fax2 = $this->input->post('fax2');
		$fax3 = $this->input->post('fax3');
		$fax = "{$fax1}-{$fax2}-{$fax3}";
		
		$mail_id = $this->input->post('mail_id');
		$mail_domain = $this->input->post('mail_domain');
		$admin_mail = "{$mail_id}@{$mail_domain}";
		
		$this->load->library('form_validation');
		$config = array(
					array('field'=> 'zipcode1','label'   => '우편번호','rules'   => 'required'),
					array('field'=> 'zipcode2','label'   => '우편번호','rules'   => 'required'),
					array('field'=> 'admin_name','label'   => '관리자명','rules'   => 'required'),
					array('field'=> 'mail_id','label'   => 'Email','rules'   => 'required'),
					array('field'=> 'mail_domain', 'label' => 'Email','rules' => 'required')
				 );
		$this->form_validation->set_rules($config);
		
		if ($this->form_validation->run() == FALSE){
			$this->alert->jalert('잘못된 접근 입니다.', '','back');
			exit;
		}else{
			switch($dbjob){
				case('i'):
					$msg = '저장되었습니다.';
					$data = array(
							'post' => $post ,
							'addr1' => $this->input->post('addr1') ,
							'addr2' => $this->input->post('addr2') ,
							'tel' => $tel,
							'fax' => $fax,
							'admin_name'=> $this->input->post('admin_name'),
							'admin_mail'=> $admin_mail,
							'minister'=> $this->input->post('minister')
					);
				break;
				case('u'):
					$msg = '수정되었습니다.';
					$data = array(
							'post' => $post ,
							'addr1' => $this->input->post('addr1') ,
							'addr2' => $this->input->post('addr2') ,
							'tel' => $tel ,
							'fax' => $fax,
							'admin_name'=> $this->input->post('admin_name'),
							'admin_mail'=> $admin_mail,
							'minister'=> $this->input->post('minister')
					);
				break;
				case('d'):
					$msg = '삭제 되었습니다.';
				break;
			}
			
			if(!empty($_FILES['file1']['tmp_name'])){
				$this->load->library('upload',array('allow_file'=>null,'allow_size'=> null,'file'=>'file1','upload_path'=>'siteinfo'));
				$this->upload->fileUpload();
				$file = $this->upload->getUploadResult();				
				$data = array_merge($data, array('photo'=> $file['real_file']));
			}
			
			$this->setting_model->siteinfo_dbjob($data);
			$this->alert->jalert($msg, base_url('/setting/siteinfo'));
		}
	}
	
	/**
	 * 관리자권한설정 
	 *
	 * @access	public
	 * @return	result
	 */
	function adminAuth(){
		login_admin();
		$url = $this->uri_church();
		$data = array(
				'_title' => '관리자권한설정',
				'_group' => 'setting',
				'_content' => $this->config->item('ch_manager_root').'/'.$url
		);
		return $data ;
	}
	
	/**
	 * 배경음악설정
	 *
	 * @access	public
	 * @return	result
	 */
	function bgm(){
		login_admin();
		$url = $this->uri_church();
		$data = array(
				'_title' => '배경음악설정',
				'_group' => 'setting',
				'_content' => $this->config->item('ch_manager_root').'/'.$url
		);
		$data = array_merge($data, $this->setting_model->select_bgm());
		return $data ;
	}
	
	/**
	 * 배경음악 DB 처리
	 *
	 * @access	public
	 * @return	result
	 */
	function bgmDbjob(){
		$dbjob = $this->input->post('dbjob');
		switch($dbjob){
			case('i'):
				$msg = '저장되었습니다.';
				$data = array(
						'start_date' => $this->input->post('start_date') ,
						'end_date' => $this->input->post('end_date') ,
						'open_yn' => $this->input->post('open_yn')
				);
				break;
			case('u'):
				$msg = '수정되었습니다.';
				$data = array(
						'start_date' => $this->input->post('start_date') ,
						'end_date' => $this->input->post('end_date') ,
						'open_yn' => $this->input->post('open_yn')
				);
				break;
			case('d'):
				//기존파일 삭제
				$bgm = $this->setting_model->select_bgm();
				if($bgm['bgm_info']){
					$old_file = explode('|',$bgm['bgm_info']);
					if(file_exists(convert_path('/upload/bgm/'.$old_file[0]))){
						@unlink(convert_path('/upload/bgm/'.$old_file[0]));
					}
				}
				$msg = '삭제 되었습니다.';
				$data = array(
						'bgm_id' => $this->input->post('id') 
				);
				break;
		}
		
		if(isset($_FILES['files'])){
			//기존파일 삭제
			$bgm = $this->setting_model->select_bgm();
			if($bgm['bgm_info']){
				$old_file = explode('|',$bgm['bgm_info']);
				if(file_exists(convert_path('/upload/bgm/'.$old_file[0]))){
					@unlink(convert_path('/upload/bgm/'.$old_file[0]));
				}
			}
			
			/*file upload begin*/
			$upload_path = convert_path('/upload/bgm');
			$config['upload_path'] = $upload_path;
			$config['allowed_types'] = '*';
			$config['max_size']	= '3072'; //3MB
			//$config['max_width']  = '1024';
			//$config['max_height']  = '768';
			$config['encrypt_name'] = true;
			$config['remove_spaces'] = true;
			$this->load->library('upload', $config);
			$this->upload->mkdir($upload_path);
			$file = $this->upload->do_multi_upload('files');
			
			$bgm_file[] = $file[0]['file_name'];
			$bgm_file[] = $file[0]['orig_name'];
			$bgm_file[] = $file[0]['file_type'];
			$bgm_file[] = $file[0]['file_size'];
			
			$bgm_info = implode('|',$bgm_file);
			$data = array_merge($data,array('bgm_info'=>$bgm_info));
		}
		
		$this->setting_model->bgminfo_dbjob($data);
		$this->alert->jalert($msg, $this->input->server('HTTP_REFERER'));
	}
	
	/**
	 * 코드관리
	 *
	 * @access	public
	 * @return	result
	 */
	function code(){
		login_admin();
		$url = $this->uri_church(2);
		$uri_array = segment_explode($this->uri->uri_string());
		$master_id = in_array('master_id', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'master_id'))) : null;		
		$data = array(
				'_title' => '코드관리',
				'_group' => 'setting',
				'_content' => $this->config->item('ch_manager_root').'/'.$url
		);
		$data = array_merge($data, array('master_id'=>$master_id));
		//$data = array_merge($data, $this->setting_model->select_code_master($master_id));
		return $data ;
	}
	
	/**
	 * 마스트 코드 db처리
	 *
	 * @access	public
	 * @return	result
	 */
	function codeMasterDbjob(){
		$dbjob = $this->input->post('dbjob');
		switch($dbjob){
			case('i'):
				$msg = '저장되었습니다.';
				$data = array(
						'title' => $this->input->post('title') ,
						'open_yn' => $this->input->post('open_yn') ,
						'etc1' => $this->input->post('etc1') ,
						'etc2' => $this->input->post('etc2') ,
						'etc3' => $this->input->post('etc3'), 
						'del_yn'=>'N'
				);
			break;
			case('u'):
				$msg = '수정되었습니다.';
				$data = array(
						'title' => $this->input->post('title') ,
						'open_yn' => $this->input->post('open_yn') ,
						'etc1' => $this->input->post('etc1') ,
						'etc2' => $this->input->post('etc2') ,
						'etc3' => $this->input->post('etc3')
				);
			break;
			case('d'):
				$msg = '삭제 되었습니다.';
				$data = array('del_yn'=>'Y');
			break;
		}
		$this->setting_model->master_code_dbjob($data);
		$this->alert->jalert($msg, base_url('/setting/code'));
	}
	
	
	/**
	 * 마스터 코드 리스트
	 *
	 * @access	public
	 * @return	result
	 */
	function ajax_master_code(){
		$url = $this->uri_church();
		$data = array(
				'_title' => '코드관리',
				'_group' => 'setting',
				'_content' => $this->config->item('ch_manager_root').'/'.$url
		);
		$data = array_merge($data, array('rst'=>$this->setting_model->list_code_master()));
		return $data;
	}
	
	/**
	 * 마스터 코드 정보
	 *
	 * @access	public
	 * @return	result
	 */
	function ajax_master_select(){
		$url = $this->uri_church();
		$id = $this->input->get('master_id');
		$data = array(
				'_title' => '코드관리',
				'_group' => 'setting',
				'_content' => $this->config->item('ch_manager_root').'/'.$url
		);
		$data = array_merge($data,$this->setting_model->select_code_master($id));
		return $data;
	}
	
	/**
	 * 차일드 코드 리스트
	 *
	 * @access	public
	 * @return	result
	 */
	function ajax_child_code(){
		$url = $this->uri_church();
		$data = array(
				'_title' => '코드관리',
				'_group' => 'setting',
				'_content' => $this->config->item('ch_manager_root').'/'.$url
		);
		$data = array_merge($data, array('rst'=>$this->setting_model->list_code_child()));
		return $data;
	}
	
	/**
	 * 차일드 코드 정보
	 *
	 * @access	public
	 * @return	result
	 */
	function ajax_child_select(){
		$url = $this->uri_church();
		$id = $this->input->get('code_id');
		$data = array(
				'_title' => '코드관리',
				'_group' => 'setting',
				'_content' => $this->config->item('ch_manager_root').'/'.$url
		);
		$data = array_merge($data,$this->setting_model->select_code_child($id));
		return $data;
	}
	
	/**
	 * 차일드 코드 db처리
	 *
	 * @access	public
	 * @return	result
	 */
	function codeChildDbjob(){
		$dbjob = $this->input->post('dbjob');
		$master_id = $this->input->post('master_id');
		switch($dbjob){
			case('i'):
				$msg = '저장되었습니다.';
				$data = array(
						'master_id' => $master_id ,
						'title' => $this->input->post('title') ,
						'open_yn' => $this->input->post('open_yn') ,
						'etc1' => $this->input->post('etc1') ,
						'etc2' => $this->input->post('etc2') ,
						'etc3' => $this->input->post('etc3'),
						'del_yn'=>'N'
				);
				break;
			case('u'):
				$msg = '수정되었습니다.';
				$data = array(
						'title' => $this->input->post('title') ,
						'open_yn' => $this->input->post('open_yn') ,
						'etc1' => $this->input->post('etc1') ,
						'etc2' => $this->input->post('etc2') ,
						'etc3' => $this->input->post('etc3')
				);
			break;
			case('d'):
				$msg = '삭제 되었습니다.';
				$data = array('del_yn'=>'Y');
			break;
			case('asc'):
				$msg = '';
				$data = null;
			break;
			case('desc'):
				$msg = '';
				$data = null;
			break;
		}
		$this->setting_model->child_code_dbjob($data);
		$this->alert->jalert($msg, base_url('/setting/code/master_id/'.$master_id));
	}
	
	/**
	 * 사이트 디자인
	 *
	 * @access	public
	 * @return	result
	 */
	function design(){
		$this->load->library('Xmlparser');
		login_admin();
		$dic = directory_map('./theme',1);
		
		$info = null;
		foreach($dic as $item){
			$xml = read_file('./theme/'.$item.'/template.xml');
			$parser = new XMLParser($xml);
			$parser->Parse();
			$info[] = array(
						'name' =>  $parser->document->title[0]->tagData,
						'code' =>  $item,
						'default' =>  $parser->document->default[0]->tagData
			);
			
		}
		
		$url = $this->uri_church();
		$data = array(
				'_title' => '사이트디자인',
				'_group' => 'setting',
				'_content' => $this->config->item('ch_manager_root').'/'.$url,
				'theme_list' => $info,
		);
		return $data ;
	}
	
	/**
	 * 사이트 디자인 폼
	 *
	 * @access	public
	 * @return	result
	 */
	function designForm(){
		login_admin();
		$url = $this->common_model->get_index_uri(2);
		$uri_array = segment_explode($this->uri->uri_string());
		$code = urldecode($this->security->xss_clean(url_explode($uri_array, 'code')));
		$skin = in_array('skin', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'skin'))) : 'main';
		
		$template = array(
					 'content' => read_file('./theme/'.$code.'/template_'.$skin.'.php'),
					 'code' => $code,
					 'skin' => $skin
		);
		
		$data = array(
				'_title' => '사이트디자인',
				'_group' => 'setting',
				'_content' => $this->config->item('ch_manager_root').'/'.$url,
				'theme' => $template
		);
		return $data ;
	}
	
	/**
	 * 사이트 디자인 미리보기
	 *
	 * @access	public
	 * @return	result
	 */
	function designPrivew(){
		login_admin();
		$url = $this->common_model->get_index_uri(2);
		$uri_array = segment_explode($this->uri->uri_string());
		$code = urldecode($this->security->xss_clean(url_explode($uri_array, 'code')));
		$skin = in_array('skin', $uri_array) ? urldecode($this->security->xss_clean(url_explode($uri_array, 'skin'))) : 'main';
		$_use_theme = ($skin == 'main') ? 'N' : 'Y'; 
		$data = array(
				'_title' => '사이트 디자인 미리보기',
				'_group' => 'setting',
				'_content' => 'content_sample',
				'_skin' => $skin,
				'_front' => 'Y'
		);
		return $data ;
	}
	
	/**
	 * 사이트 디자인 수정
	 *
	 * @access	public
	 * @return	void
	 */
	function designModify(){
		$code = $this->input->post('code');
		$skin = $this->input->post('skin');
		$content = $this->input->post('template');
		write_file(convert_path('/theme/'.$code.'/template_'.$skin.'.php'), $content);
		$this->alert->jalert('수정되었습니다.', base_url('/setting/designForm/code/'.$code.'/skin/'.$skin));
	}
	
	/**
	 * 사이트 사용/미사용 변경
	 *
	 * @access	public
	 * @return	void
	 */
	function designDefault(){
		login_admin();
		$this->load->library('Xmlparser');
		$this->load->library('Simplexml');
		
		$code = $this->input->post('code');
		$defalut_yn = $this->input->post('defalut_yn');
		
		$dic = directory_map('./theme',1);
	
		$be_success = true;
		foreach($dic as $item){
			$xml = read_file('./theme/'.$item.'/template.xml');
			$parser = new XMLParser($xml);
			$parser->Parse();

			if($defalut_yn == 'Y' && $parser->document->code[0]->tagData != $code && $parser->document->default[0]->tagData =='Y'){
				$be_success = false;
				break;
			}	
		}
		
		$msg = '이미 사용중인 테마가 존재합니다.\n기존 테마 상태를 미사용으로 변경후 다시 시도해 주세요';
		if($be_success){
			$msg = '적용되었습니다.';
			
			$doc = new DOMDocument;
			$doc->load('./theme/'.$code.'/template.xml');
			$book = $doc->documentElement;
			
			// Locate the old parent node
			$xpath = new DOMXpath($doc);
			$nodelist = $xpath->query('/root/default');
			$oldnode = $nodelist->item(0);
			$newelement = $doc->createElement('default', $defalut_yn);
			$nodelist->item(0)->parentNode->replaceChild($newelement, $oldnode);
			
			$xml = write_file(convert_path('/theme/'.$code.'/template.xml'),$doc->saveXML());
					}
		$this->alert->jalert($msg, base_url('/setting/design'));
	}
			
	/**
	 * 이용중인서비스
	 *
	 * @access	public
	 * @return	result
	 */
	function service(){
		login_admin();
		$url = $this->uri_church();
		$data = array(
				'_title' => '이용중인서비스',
				'_group' => 'setting',
				'_content' => $this->config->item('ch_manager_root').'/'.$url
		);
		return $data ;
	}
	
	
	
	
	
}

/* End of file html.php */
/* Location: ./application/controllers/setting.php */